<?php
$success = true;
if (isset($_POST['base'])) {
    include_once 'admin/common.func.php';
    $base = trim($_POST['base']);
    if (substr($base, -1) == '/') {
        $base = substr($base, 0, -1);
    }
    //install settings
    $settings = getSettings();
    $settings['base'] = $base;
    if (!saveSettings($settings)) {
        $success = false;
    }
    //install nav
    $navs = array(array('text' => '首页', 'href' => $base, 'target' => '_self'));
    if (!saveNavs($navs)) {
        $success = false;
    }
    //install category
    $categorys = array(array('name' => '未分类', 'posts' => array()));
    if (!saveCategorys($categorys)) {
        $success = false;
    }
    //install link
    $links = array(array('content' => 'Paper Blog', 'href' => 'http://paperblog.showframework.com/', 'title' => 'Paper Blog'));
    if (!saveLinks($links)) {
        $success = false;
    }
    //add first blog instead of next two step
    //install index
    $index = array();
    $success = savePosts($index) && $success;
    //install tag
    $tag = array('paperblog' => array('name' => 'paperblog', 'posts' => array()));
    $success = saveTags($tag) && $success;
} else {
    $success = false;
}
?><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Paper Blog Install</title>
</head>
<body>
    <?php
    echo sprintf('<div>当前文件 %s</div>', __FILE__);
    echo sprintf('<div>当前目录 %s</div>', dirname(__FILE__));
    echo sprintf('<div>当前目录是否可写 %s</div>', is_writable(dirname(__FILE__)));
    echo sprintf('<div>分类目录是否可写 %s</div>', is_writable(dirname(__FILE__) . '/category'));
    echo sprintf('<div>标签目录是否可写 %s</div>', is_writable(dirname(__FILE__) . '/tag'));
    echo sprintf('<div>文章目录是否可写 %s</div>', is_writable(dirname(__FILE__) . '/post'));
    echo sprintf('<div>数据目录是否可写 %s</div>', is_writable(dirname(__FILE__) . '/data'));
    echo sprintf('<div>数据posts目录是否可写 %s</div>', is_writable(dirname(__FILE__) . '/data/posts'));
    ?>
    <?php
    if ($success) {
        unlink(__FILE__);
        echo sprintf('<div>安装成功 &nbsp;<a href="%s/admin/setting.php">博客配置</a></div>', $base);
    }
    ?>
    <form action="install.php" method="POST">
        <label for="base">博客地址：</label><input type="text" name="base" id="base" /><span>例如：http://paperblog.showframework.com</span>
        <input type="submit" value="安装" />
    </form>
</body>
</html>
